package com.ihg.hiex.admin.service.session.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.utils.CollectionUtils;
import com.ihg.hiex.admin.dao.session.FinalTestDao;
import com.ihg.hiex.admin.service.session.FinalTestService;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.session.before.finaltest.ExerciseType;
import com.ihg.hiex.entity.session.before.finaltest.FinalTest;
import com.ihg.hiex.entity.session.before.finaltest.FinalTestExercise;
import com.ihg.hiex.entity.user.Assessor;

/**
 *
 * @author Ark
 *
 */
@Service("finalTestService")
public class FinalTestServiceImpl implements FinalTestService {
    
    @Dao
    private FinalTestDao finalTestDao;
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.FinalTestService#list(com.ihg.hiex.entity.user.Assessor)
     */
    public List<FinalTest> list(Assessor assessor) {
        return finalTestDao.list(assessor);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.FinalTestService#construct(com.ihg.hiex.entity.session.Session)
     */
    public Map<String, FinalTest> list(Session session) {
        
        List<FinalTest> finalTests = finalTestDao.list(session);
        if (org.apache.commons.collections.CollectionUtils.isEmpty(finalTests)) {
            return Collections.emptyMap();
        }
        
        for (FinalTest finalTest : finalTests) {
            
            List<FinalTestExercise> basicExercises = finalTestDao.listExercise(session, finalTest.getAssessor(), ExerciseType.BASIC);
            int countReBasicExercise = this.getReExerciseCount(basicExercises);
            
            List<FinalTestExercise> enhanceExercises = finalTestDao.listExercise(session, finalTest.getAssessor(), ExerciseType.ENHANCE);
            int countReEnhanceExercise = this.getReExerciseCount(enhanceExercises);
            
            finalTest.reExerciseCount(countReBasicExercise + 1, countReEnhanceExercise + 1);
            
        }
        
        return CollectionUtils.constructMapValue(finalTests, "assessor.id");
        
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.FinalTestService#listExercise(com.ihg.hiex.entity.session.Session)
     */
    public Map<String, List<FinalTestExercise>> listExercise(Session session) {
        List<FinalTestExercise> exercises = finalTestDao.listExercise(session);
        if (org.apache.commons.collections.CollectionUtils.isEmpty(exercises)) {
            return Collections.emptyMap();
        }
        
        return CollectionUtils.constructMapValues(exercises, "assessor.id");
    }
    
    /**
     * @param exercises
     * @return
     */
    private int getReExerciseCount(List<FinalTestExercise> exercises) {
        int count = 0;
        for (FinalTestExercise exercise : exercises) {
            int reExerciseCount = exercise.getReExerciseCount();
            if (reExerciseCount > count) {
                count = reExerciseCount;
            }
        }
        return count;
    }
    
}
